# Maintainer: wirx6 <wirx654@gmail.com>

_realname=aom
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.5.0
pkgrel=1
pkgdesc='AV1 codec library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://aomedia.org/"
license=('spdx:BSD-2-Clause')
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("perl"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             $( [[ "${CARCH}" != "i686" \
                   && "${CARCH}" != "x86_64" ]] \
                || echo "${MINGW_PACKAGE_PREFIX}-yasm" )
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://storage.googleapis.com/aom-releases/libaom-${pkgver}.tar.gz"{,.asc})
sha256sums=('d37dbee372e2430a7efde813984ae6d78bdf1fc4080ebe32457c9115408b0738'
            'SKIP')
validpgpkeys=('B002F08B74A148DAA01F7123A48E86DB0B830498')

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ "${CARCH}" == "aarch64" ]]; then
      extra_config+=("-DCONFIG_RUNTIME_CPU_DETECT=0")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DENABLE_TESTS=OFF \
      ../libaom-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 ${srcdir}/libaom-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/libaom-${pkgver}/PATENTS ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/PATENTS
}
